package com.sgcc.pda.bluetooth.utils;

import android.text.TextUtils;

import com.google.gson.Gson;
import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import com.google.gson.TypeAdapter;
import com.google.gson.TypeAdapterFactory;
import com.google.gson.reflect.TypeToken;

import java.io.IOException;
import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.List;

/**
 * 描 述：json数据解析
 * 作 者：yangzhi
 * 时 间：2020/3/5 15:15
 * 版 本：1.0
 * 版 权： Copyright (c) remarkables-united.Co.Ltd. All rights reserved.
 */
public class ToolGson {

    /**
     * 获取json数据
     *
     * @param jsonString json格式的字符串
     * @param token
     * @param <T>
     * @return
     */
    public static <T> T getBean(String jsonString, TypeToken<T> token) {
        T t = null;
        try {
            Gson gson = new Gson();
            t = gson.fromJson(jsonString, token.getType());
        } catch (Exception e) {
            e.printStackTrace();
        }
        return t;
    }

    public static <T> T getBean(String jsonString, Class<T> cls) {
        T t = null;
        try {
            Gson gson = new Gson();
            t = gson.fromJson(jsonString, cls);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return t;
    }

    public static <T> List<T> getBeans(String jsonString, final Class<T> cls) {
        List<T> list = new ArrayList<>();
        try {
            Gson gson = new Gson();
            JsonArray arry = new JsonParser().parse(jsonString).getAsJsonArray();
            for (JsonElement jsonElement : arry) {
                list.add(gson.fromJson(jsonElement, cls));
            }
        } catch (Exception e) {
            e.printStackTrace();

        }
        return list;
    }

    public static <T> ArrayList<T> jsonToArrayList(String json, Class<T> clazz) {
        Type type = new TypeToken<ArrayList<JsonObject>>() {
        }.getType();
        ArrayList<JsonObject> jsonObjects = new Gson().fromJson(json, type);
        ArrayList<T> arrayList = new ArrayList<>();
        for (JsonObject jsonObject : jsonObjects) {
            arrayList.add(new Gson().fromJson(jsonObject, clazz));
        }
        return arrayList;
    }


    public static String getJsonStr(Object obj) {
        String result = null;
        try {
            Gson gson = new Gson();
            result = gson.toJson(obj);
        } catch (Exception e) {
            e.printStackTrace();

        }
        return result;
    }

    @SuppressWarnings("TypeParameterHidesVisibleType")
    private static class NullStringToEmptyAdapterFactory<T> implements TypeAdapterFactory {
        @Override
        public <T> TypeAdapter<T> create(Gson gson, TypeToken<T> type) {
            Class<T> rawType = (Class<T>) type.getRawType();
            if (!rawType.equals(String.class)) {
                return null;
            }
            return (TypeAdapter<T>) new StringNullAdapter();
        }
    }

    static class StringNullAdapter extends TypeAdapter<String> {

        @Override
        public void write(com.google.gson.stream.JsonWriter writer, String value) throws IOException {
            if (TextUtils.isEmpty(value)) {
                writer.value("null");
                return;
            }
            writer.value(value);
        }

        @Override
        public String read(com.google.gson.stream.JsonReader reader) throws IOException {
            switch (reader.peek()){
                case NULL:
                    reader.nextNull();
                return "";
            }
            return reader.nextString();
        }
    }

}
